<?php

namespace app\index\controller;


use app\common\entity\Config;
use app\common\entity\Goods;
use app\common\entity\MachineList;
use app\common\entity\MywalletLog;
use app\common\entity\Orders;
use app\common\entity\Proportion;
use app\common\entity\SendMachine;
use app\common\entity\User;
use app\common\entity\UserLevelSend;
use app\common\entity\UserPackage;
use app\common\entity\Video;
use app\common\entity\WithdrawRatio;
use think\Db;
use think\Request;
use app\common\entity\ProLog;
use app\common\entity\OtcMatch;
use app\common\shop\UserAgentLevelModel;



class Timechick
{



    public function xxx($mobile,$p_nick_name,$num)
    {


    }


    /**
     * 判断是不是机器人
     * @param $uid
     * @return bool
     */
    static public function is_robot($uid){
        if(!$uid){
            return false;
        }
        $robot = User::where('id',$uid)->where('robot',1)->find();
        return $robot?true:false;
    }
    /**
     * 获取上级代理商
     * @param $data
     * @param $id
     * @return array
     */
    static public function fanTree($data, $id,$level){
        $list = array();
        foreach($data as $v) {
            if($v['id'] == $id ) {
                if($level < $v['agent_level'] || $level == 0) {

                    $v['type'] = 'percentage';
                    $v['type_name'] = '佣金';
                    $item = self::fanTree($data, $v['pid'], $v['agent_level']);
                    array_push($list, $v);
                    if (!empty($item)) {
                        $list = array_merge($list, $item);
                    }
                }else{
                    if($v['agent_level'] > 0 && $level == $v['agent_level']){

                        $v['type'] = 'peers';
                        $v['type_name'] = '平级奖';
                        array_push($list, $v);
                    }
                }

            }
        }
        return $list;
    }



    #定时更新平台行情
    public function updPet()
    {

        $proModel = new Proportion();
        $proLogModel = new ProLog();


        $todayInfo = $proModel->where('date', '<=', date('Y-m-d H:i:s', time()))->order('date desc')->find();


        $dayRatio = $todayInfo['day_ratio'] / 100;//涨跌幅

        $todayNum = $todayInfo['today_ratio'];

        $nowRatio = $todayInfo['ratio'];//现在价格

        if ($dayRatio != 0) {

            $dayUpDowm = bcmul($todayNum, $dayRatio, 4);//总共涨跌数量

            $dayUp = bcadd($todayNum, $dayUpDowm, 4);//最高/最低

            $oneUp = bcmul($todayNum, 0.001, 8);
            $oneUp = bcmul($oneUp, $todayInfo['ratio_times'], 8);



            if ($dayRatio > 0) {

                $upNum = bcadd($nowRatio, $oneUp, 4);
            } else {
                $upNum = bcsub($nowRatio, $oneUp, 4);

            }


            if ($dayUp > $todayNum) {
                //涨
                if ($upNum >= $dayUp) {
                    $upNum = $dayUp;
                }

            } else {

                if ($upNum <= $dayUp) {
                    $upNum = $dayUp;
                }

            }

            /*if ($todayNum > $dayUp) {
                //跌
                $upNum = mt_rand($dayUp * 10000, $todayNum * 10000);

            } else {
                //涨
                $upNum = mt_rand($todayNum * 10000, $dayUp * 10000);

            }*/

            //$upNum = $upNum / 10000;
        } else {
            $upNum = $todayNum;
        }


        $todayHas = $proModel->whereTime('date', 'today')->find();

        if (!$todayHas) {

            $data = [
                'date' => date('Y-m-d'),
                'ratio' => $todayInfo['ratio'],
                'today_ratio' => $todayInfo['ratio'],
                'day_max' => $todayInfo['day_max'],
                'day_min' => $todayInfo['day_min'],
            ];

            $proModel->insert($data);

        }


        //增加记录
        $proLogModel->add($upNum);

        $max = $proLogModel->whereTime('date', 'today')->max('ratio');
        $min = $proLogModel->whereTime('date', 'today')->min('ratio');

        $updData = [
            'ratio' => $upNum,
            'day_max' => $max,
            'day_min' => $min,
        ];

        $max_min = $proModel->whereTime('date', 'today')->update($updData);
        return 'ok';

    }

    public function match(){
        $time = time()-Config::getValue('time_end')*3600;
        //未付款订单直接作废
        OtcMatch::where('status',1)->where('create_time','<',$time)->update(['status'=>4]);
        //已付款订单直接完成
        $matchs = OtcMatch::where('status',2)->where('create_time','<',$time)->select();
        foreach($matchs as $k=>$v){
            OtcMatch::complete($v['id']);
        }
    }
    public function temp(){
        $todayInfo = MywalletLog::where('types',3)->whereTime('create_time','week')->find();

        if($todayInfo){
            return '今天已经奖励过了';
        }
        $users = User::where('level','>',0)->select();

        foreach($users as $k=>$v){
            $find = Db::name('user')->where('pid',$v['id'])->select();
            $find = implode(',',array_column($find,'id'));
            $today = Orders::alias('o')->field('sum(o.pay_money) as sum_money,sum(g.gyg) as gyg')
                ->join('goods g','g.id = o.goods_id')
                ->whereTime('o.pay_time','week')
                ->where('o.user_id','in',$find)
                ->where('o.status','in','1,2,3')
                ->group('o.user_id')
                ->order('sum_money desc')
                ->select();

            if(!isset($today[1])){
                continue;
            }
            unset($today[0]);
            $minSum = 0;
            foreach($today as $key=>$val){
                $minSum += $val['gyg'];
            }
            $percent = UserLevelSend::getProfit($v['level'],'team_ratio');
            $num = bcmul($minSum,bcmul($percent,0.01,4),4);
            MywalletLog::lockToGyg($v['id'],$num,'团队加速释放',3);

            echo $v['id'].'奖励'.$num."\n";
        }
    }



    public function upLevel(){
        $users = User::where('real_pass',2)->select();
        foreach($users as $k=>$v){
            print_r(User::upLevel($v['id']));
        }
    }


    public function del_match(){
        $time = time()-86400;
        OtcMatch::where('create_time','<',$time)->where('status',1)->update(['status'=>4,'end_time'=>time()]);
    }

}